﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.PublicShow
{
    public partial class SelectDeps : BasePage
    {
        private Sys_CompanyBLL scb = new Sys_CompanyBLL();
        private Sys_UserBLL sub = new Sys_UserBLL();
        private Sys_DepartmentBLL sdb = new Sys_DepartmentBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["mode"].ToString()))
                {
                    string nems = Request["mode"].ToString();
                    string SelectType = (Request["mode"].ToString() == "1" ? "单选" : "多选");
                }

                string name = "";
                string cid = "";
                DataTable dt = scb.GetAllInfo(" AND CMY_PARENTID=0");

                foreach (DataRow dr in dt.Rows)
                {
                    name = dr["CMY_NAME"].ToString();
                    cid = dr["CMY_ID"].ToString();
                    string unit = "【" + "总公司" + "】";
                    TreeNode t = new TreeNode(name + unit, cid);
                    MyTree.Nodes.Add(t);
                    LoadComNode(Convert.ToInt32(cid), t);
                    MyTree.ExpandDepth = 1;
                }
            }
        }

        //获取公司节点的递归方法
        private void LoadComNode(int parent, TreeNode pNode)
        {
            DataTable dataDep = sdb.GetDataAllInfo(@"AND d.CMY_ID = " + parent + " AND ( d.DEPTPARENTID = 0 or d.DEPTPARENTID =-1) "); //公司直属部门
            if (dataDep.Rows.Count > 0)
            {
                for (int i = 0; i < dataDep.Rows.Count; i++)
                {
                    string unit = "【" + "部门" + "】";

                    TreeNode node = new TreeNode(dataDep.Rows[i]["DEPTNAME"].ToString() + unit, dataDep.Rows[i]["DEPTID"].ToString());
                    pNode.ChildNodes.Add(node);
                    LoadDepNode(Convert.ToInt32(node.Value), node);
                }
            }

            DataTable dataCom = scb.GetAllInfo("AND CMY_PARENTID=" + parent);
            if (dataCom.Rows.Count > 0)
            {
                for (int i = 0; i < dataCom.Rows.Count; i++)
                {
                    string unit = "【" + "公司" + "】";

                    TreeNode node = new TreeNode(dataCom.Rows[i]["CMY_NAME"].ToString() + unit, dataCom.Rows[i]["CMY_ID"].ToString());
                    pNode.ChildNodes.Add(node);
                    LoadComNode(Convert.ToInt32(node.Value), node);
                }
            }
        }

        //获取部门
        private void LoadDepNode(int parentDid, TreeNode pNode)
        {
            DataTable dataDep = sdb.GetDataAllInfo(@" AND d.DEPTPARENTID = " + parentDid + " "); //下级部门
            if (dataDep.Rows.Count > 0)
            {
                for (int i = 0; i < dataDep.Rows.Count; i++)
                {
                    string unit = "【" + "部门" + "】";

                    TreeNode node = new TreeNode(dataDep.Rows[i]["DEPTNAME"].ToString() + unit, dataDep.Rows[i]["DEPTID"].ToString());
                    pNode.ChildNodes.Add(node);
                    LoadDepNode(Convert.ToInt32(node.Value), node);

                    if (Request["ckdep"] != null)
                    {
                        if (("," + Request["ckdep"].ToString() + ",").IndexOf("," + dataDep.Rows[i]["DEPTID"].ToString() + ",") >= 0)
                        {
                            node.Checked = true;
                        }
                    }
                }

                if (Session["CheckedDeps"] != null)
                {
                    Session["CheckedDeps"] = null;
                }
            }
        }

        private void BindUser(TreeNode tn, string cid, string did)
        {
            DataTable dataUser = sub.GetAllUser(@" AND USER_COMPANY=" + cid + " AND USER_DEPARTMENT=" + did); //公司直属部门
            TreeNode tr = null;
            if (dataUser != null)
            {
                for (int i = 0; i < dataUser.Rows.Count; i++)
                {
                    tr = new TreeNode();
                    tr.Value = dataUser.Rows[i]["USER_ID"].ToString();
                    tr.Text = dataUser.Rows[i]["USER_NAME"].ToString() + "【人员】";
                    tn.ChildNodes.Add(tr);
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tnc = MyTree.CheckedNodes;
            try
            {
                int count = 0;
                string Ids = "";
                string Name = "";
                string SelectType = (Request["mode"].ToString() == "1" ? "单选" : "多选");
                foreach (TreeNode item in tnc)
                {
                    if (item.Checked)
                    {
                        bool na0 = item.Text.Contains("【公司】");
                        bool na1 = item.Text.Contains("【部门】");

                        bool na2 = false;
                        if (item.ChildNodes.Count > 0)
                        {
                            na2 = true;
                        }

                        if (na1 == true && na2 == false)
                        {
                            count += 1;
                            Name += item.Text.Substring(0, item.Text.IndexOf("【")) + ",";
                            Ids += item.Value + ",";
                        }
                    }
                }
                if (SelectType == "单选" && count != 1)
                {
                    MessageBox.Show(Page, "请选择一个部门");
                    return;
                }
                if (count <= 0)
                {
                    MessageBox.Show(Page, "请至少选择一个部门");
                    return;
                }
                Name = Ids.Substring(0, +Ids.LastIndexOf(',')) + "|" + Name.Substring(0, Name.LastIndexOf(','));
                MessageBox.ResponseScript(Page, "window.returnValue='" + Name + "';window.opener=null;window.close();");
                MessageBox.ResponseScript(Page, "if( window.opener=undefined) {  window.opener.returnValue='" + Name + "'; } else {  window.returnValue='" + Name + "' ;  window.opener=null;}  window.close();");
            }
            catch (Exception exp)
            {
                MessageBox.Show(Page, exp.Message.ToString());
                return;
            }
        }
    }
}